﻿//配置插件目录
layui.config({
    base: '/mods/'
});

layui.use(['form', 'laydate', 'util', 'jquery', 'layer', 'table', 'upload', 'dropdown'], function () {
    var form = layui.form;
    var upload = layui.upload;
    var table = layui.table;
    var dropdown = layui.dropdown;
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var laydate = layui.laydate;
    var layer = layui.layer; // 添加layui弹出层模块
    var Id = location.search.substring(2).split("=")[1];

    $(function () {
        GetInfo();
    })
    function GetInfo() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventGet?Id=" + Id,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                clickTree();
                $("#TypeInfo").val(res.PId);
                $("#TypeId").val(res.TypeId);
                $("#TypeName").val(res.TypeName);
                $("#EventDesc").val(res.EventDesc);
                $("#Image").val(res.EventImage);
                SelLevel();
                $("#EventLevel").val(res.EventLevel);
                SelTime();
                $("#EventTimeLimit").val(res.EventTimeLimit);
                form.render('select');
            }
        })
    }
    function SelLevel() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleLevel",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#EventLevel");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.LevelName).text(item.LevelName))
                })
                layui.form.render('select');
            }
        })
    }
    function SelTime() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleTimeLimit",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#EventTimeLimit");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.LimitName).text(item.LimitName))
                })
                layui.form.render('select');
            }
        })
    }
    function clickTree() {
        $.ajax({
            url: "http://8.140.245.229:8000/routechen/api/ReadEvent/HandleEventSelect",
            async: false,
            type: "get",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            success: function (res) {
                var select = $("#TypeInfo");
                $.each(res, function (index, item) {
                    select.append($('<option>').val(item.EventTypeName).text(item.EventTypeName));
                })
                layui.form.render('select');
                //设置对应 id 的节点选中
            }
        })
    }
    upload.render({
        elem: '#ID-upload-demo-btn',
        headers: {
            'Authorization': "Bearer " + localStorage["token"] + ""
        },
        url: 'http://8.140.245.229:8000/webroute/api/Event/UpFile', // 实际使用时改成您自己的上传接口即可。
        done: function (res) {

            $('#Image').attr('src', 'http://8.140.245.229:8000/webroute/File/' + res);
            console.log($('#Image'));
        },
    })

    form.on('submit(demo1)', function (data) {
        var obj = {};
        var field = data.field;
        obj.EventId = Id;
        obj.PId = field.TypeInfo;
        obj.TypeId = field.TypeId;
        obj.TypeName = field.TypeName;
        obj.EventDesc = field.EventDesc;
        obj.EventImage = field.Image;
        obj.EventLevel = field.EventLevel;
        obj.EventTimeLimit = field.EventTimeLimit;
        $.ajax({
            url: "http://8.140.245.229:8000/webroute/api/Event/HandleUpdEvent",
            type: "put",
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            data: JSON.stringify(obj),
            contentType: "application/json",
            success: function (res) {
                if (res > 0) {
                    layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                    }, function () {
                        location.href = "/Event/EventManage";
                    });

                } else {
                    layer.alert("修改失败");
                }
            }
        })
        return false; // 阻止默认 form 跳转
    })
})
function RestEmployee() {
    $(".layui-form").reset();
    layui.form.render();
}